package org.sundatagroup.aurora.monitor.entity.pojo;

import java.util.ArrayList;
import java.util.List;

import org.sundatagroup.aurora.core.entity.IJsonEntity;
import org.sundatagroup.aurora.monitor.entity.model.PlayListModel;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

/**
 * <p>服务器使用的播放列表
 * </p>
 * @author CharlesChen
 * @date 2018-05-02
 * @since 0.1.1
 */
public class PlayList implements IJsonEntity{
	private int lid;
	private String name;
	private String desc;
	private List<Integer> projects;

	public PlayList() {
		
	}

	/**
	 * 验证参数
	 * @throws IllegalArgumentException
	 * @return bool
	 */
	public boolean isValid() {
		if (lid > 0 && name.length() != 0 && projects.size() !=0) {
			return true;
		} else {
			throw new IllegalArgumentException("PlayList参数配置有误，请检查！");
		}
	}

	/**
	 * 转换{@link PlayList}为{@link PlayListModel}
	 * @return
	 */
	public PlayListModel toModel() {
		PlayListModel model = new PlayListModel();
		model.setLid(lid);
		model.setName(name);
		model.setDesc(desc);
		
		if (projects != null) {
			model.setProjects(projects.subList(0, projects.size()));
		} else {
			model.setProjects(new ArrayList<>());
		}

		return model;
	}

	/**
	 * {@link PlayListModel}转换为{@link PlayList}
	 * @param model
	 * @return
	 */
	public static PlayList fromModel(PlayListModel model) {
		PlayList playList = new PlayList();
		playList.setName(model.getName());
		playList.setLid(model.getLid());
		playList.setDesc(model.getDesc());
		
		List<Integer> a = model.getProjects();
		if (a != null) {
			playList.setProjects(a.subList(0, a.size()));
		} else {
			playList.setProjects(new ArrayList<>());
		}

		return playList;
	}

	@Override
	public JsonObject toJson() {
		JsonObject jo = new JsonObject();
		jo.addProperty("lid", lid);
		jo.addProperty("name", name);
		jo.addProperty("desc", desc);
		
		JsonArray a = new JsonArray();
		if (projects != null) {
			int len = projects.size();
			for (int i = 0; i < len; i++) {
				a.add(projects.get(i));
			}
		}
		jo.add("projects", a);

		return jo;
	}

	public int getLid() {
		return lid;
	}

	public void setLid(int lid) {
		this.lid = lid;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	public List<Integer> getProjects() {
		return projects;
	}

	public void setProjects(List<Integer> projects) {
		this.projects = projects;
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
		if (o == null || getClass() != o.getClass()) return false;

		PlayList playList = (PlayList) o;

		if (lid != playList.lid) return false;
		if (!name.equals(playList.name)) return false;
		if (desc != null ? !desc.equals(playList.desc) : playList.desc != null) return false;
		return projects != null ? projects.equals(playList.projects) : playList.projects == null;
	}

	@Override
	public int hashCode() {
		int result = lid;
		result = 31 * result + name.hashCode();
		result = 31 * result + (desc != null ? desc.hashCode() : 0);
		result = 31 * result + (projects != null ? projects.hashCode() : 0);
		return result;
	}

	@Override
	public String toString() {
		return "PlayList{" +
				"lid=" + lid +
				", name='" + name + '\'' +
				", desc='" + desc + '\'' +
				", projects=" + projects +
				'}';
	}
}
